package com.woniuxy.service.impl;

import com.woniuxy.dao.ManagerLoginDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ManagerLoginService;
import com.woniuxy.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 登录用的业务层   核心业务的实现
 */
@Service
public class ManagerLoginServiceImpl implements ManagerLoginService {
    @Autowired(required = false)
    ManagerLoginDao managerLoginDao;

    @Override
    public ResponseEntity login(RbacManager rbacManager) {
        int num = managerLoginDao.login(rbacManager);
        if (num>0){
            //如果数据库有数据，则我们要使用JWTUtil创建token
            String token = JWTUtil.sign(rbacManager.getAccount(), rbacManager.getPassword());
            return new ResponseEntity(200,"登录成功",token);
        }else {
            return new ResponseEntity(403,"账号或密码错误",null);
        }
    }

    @Override
    public String findPassword(String username) {
        return  managerLoginDao.findPassword(username);
    }

    @Override
    public Set<String> findAllRoles(String username) {
        return managerLoginDao.findAllRoles(username);
    }

    @Override
    public Set<String> findAllPerms(String username) {
        return managerLoginDao.findAllPerms(username);
    }
}
